package com.wl.network;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLTest {
    public static void main(String[] args) {
        try {
            //下载地址
            URL url = new URL("https://m701.music.126.net/20210718183300/f6d81b339d85478f074cca89cc209dcc/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/9875610543/fb9e/1ebd/a9f4/a02692d18dc64731b0cdb82b3d7840cb.m4a");
            //连接到这个资源
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            FileOutputStream fos = new FileOutputStream("lovely.m4a");
            byte[] buffer = new byte[1024];
            int len;
            while ((len = inputStream.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            inputStream.close();
            urlConnection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
